package com.woniuxy.huayoung.service.impl;

import com.woniuxy.huayoung.entity.TravelNotes;
import com.woniuxy.huayoung.mapper.TravelNotesMapper;
import com.woniuxy.huayoung.mapper.UserMapper;
import com.woniuxy.huayoung.service.TravelNotesService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TravelNotesServiceImpl implements TravelNotesService {
    @Resource
    TravelNotesMapper travelNotesMapper;

    @Resource
    UserMapper userMapper;

    //根据点击量排名查找游记
    @Override
    public List<TravelNotes> findByTopClicks(int top) {
        List<TravelNotes> travelNotesList = travelNotesMapper.findByTopClicks(top);
        return travelNotesList;
    }

    @Override
    public List<String> findImgs() {
        List<TravelNotes> byTopClicks = travelNotesMapper.findByTopClicks(5);
        List<String> list = new ArrayList<>();
        for (TravelNotes travelNotes : byTopClicks) {
            list.add(travelNotes.getCoverImage());
        }
        return list;
    }

    //根据省份导航展示对应省份游记
    @Override
    public List<TravelNotes> findByLocation(String location) {
        List<TravelNotes> travelNotesList = travelNotesMapper.findByLocation(location);
        return travelNotesList;
    }

    //根据搜索词查找游记
    @Override
    public List<TravelNotes> findByWords(String words) {
        List<TravelNotes> results = travelNotesMapper.findByWords(words);
        return results;
    }

    //游记模块显示最热游记
    @Override
    public List<TravelNotes> showHottest() {
        List<TravelNotes> hottest = travelNotesMapper.findByHottest();
        return hottest;
    }

    //根据ID查游记
    @Override
    public TravelNotes showDetails(int id) {
        TravelNotes travelNotes = travelNotesMapper.findById(id);
        travelNotes.setUsername(userMapper.findById(travelNotes.getUid()).getAccount());
        return travelNotes;
    }

    //编辑游记
    @Override
    public int editTravelNotes(TravelNotes travelNotes) {
        String htmlContent = travelNotes.getContent();
        Jsoup.parse(htmlContent);
        //Jsoup解析html
        Document document = Jsoup.parse(htmlContent);
        //获取第一个图片元素
        Element firstImg = document.getElementsByTag("img").first();
        if(firstImg==null){
            travelNotes.setCoverImage(null);
        }else {
        //将element转换成String并存入travelNotes
        travelNotes.setCoverImage(firstImg.toString());}
        //获取文本内容并存入travelNotes
        travelNotes.setPreface(document.text());
        System.out.println("fffffffffffffffffffffffff");
        System.out.println(travelNotes);
        int row = travelNotesMapper.updateById(travelNotes);
        return row;
    }

    //根据ID删除游记
    @Override
    public int deleteTravelNotes(int id) {
        int row = travelNotesMapper.deleteById(id);
        return row;
    }

    //增加游记
    @Override
    public int insert(TravelNotes travelNotes) {
        String htmlContent = travelNotes.getContent();
        Jsoup.parse(htmlContent);
        //Jsoup解析html
        Document document = Jsoup.parse(htmlContent);
        //获取第一个图片元素
        Element firstImg = document.getElementsByTag("img").first();
        if(firstImg==null){
            travelNotes.setCoverImage(null);
        }else {
            //将element转换成String并存入travelNotes
            travelNotes.setCoverImage(firstImg.toString());}
        //获取文本内容并存入travelNotes
        travelNotes.setPreface(document.text());
        int row=travelNotesMapper.insert(travelNotes);
        return row;
    }

    @Override
    public int addClicks(int id) {
        int row = travelNotesMapper.updateClicksById(id);
        return row;
    }


    //测试jsoup
//    public void test(){
//        TravelNotes tn = travelNotesMapper.findById(13);
//        String html = tn.getContent();
//        //Jsoup解析html
//        Document document = Jsoup.parse(html);
//        //像js一样，通过标签获取title
//        System.out.println(document.getElementsByTag("img").first());
//    }




    @Override
    public List<TravelNotes> findByUid(int uid) {
        return travelNotesMapper.findByUid(uid);
    }

    @Override
    public int delById(int id) {
        return travelNotesMapper.delById(id);
    }
}
